package com.ruoyi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.ProductCategory;
import com.ruoyi.order.service.IProductCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物品目录Controller
 * 
 * @author lzp
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/order/category")
public class ProductCategoryController extends BaseController
{
    @Autowired
    private IProductCategoryService productCategoryService;

    /**
     * 查询物品目录列表
     */

    @GetMapping("/listByType/{categoryType}")
    public AjaxResult listByCategoryType(@PathVariable String categoryType) {
        ProductCategory query = new ProductCategory();
        query.setType(categoryType);  // 假设 ProductCategory 有 type 字段
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(query);
        return AjaxResult.success(list);
    }

    @GetMapping("/listByProductId/{productId}")
    public AjaxResult listByProductId(@PathVariable String productId) {
        ProductCategory query = new ProductCategory();
        query.setProductId(productId);
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(query);
        return AjaxResult.success(list);
    }


    @GetMapping("/list")
    public TableDataInfo list(ProductCategory productCategory)
    {
        startPage();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return getDataTable(list);
    }

    /**
     * 导出物品目录列表
     */
    @PreAuthorize("@ss.hasPermi('order:category:export')")
    @Log(title = "物品目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategory productCategory)
    {
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        ExcelUtil<ProductCategory> util = new ExcelUtil<ProductCategory>(ProductCategory.class);
        util.exportExcel(response, list, "物品目录数据");
    }

    /**
     * 获取物品目录详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") String productId)
    {
        return success(productCategoryService.selectProductCategoryByProductId(productId));
    }

    /**
     * 新增物品目录
     */
    @PreAuthorize("@ss.hasPermi('order:category:add')")
    @Log(title = "物品目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    /**
     * 修改物品目录
     */
    @PreAuthorize("@ss.hasPermi('order:category:edit')")
    @Log(title = "物品目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 删除物品目录
     */
    @PreAuthorize("@ss.hasPermi('order:category:remove')")
    @Log(title = "物品目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable String[] productIds)
    {
        return toAjax(productCategoryService.deleteProductCategoryByProductIds(productIds));
    }
}
